//
//  HomeViewController.swift
//  DYZB
//
//  Created by HOu on 2021/11/18.
//

import UIKit

private let kTitleViewH: CGFloat = 40

class HomeViewController: UIViewController {

    //懒加载属性
    private lazy var papTitleView :PageTitleView = {[weak self] in
        let titleFrame = CGRect(x: 0, y: kStatuseBarH + kNavigationBarH, width: kScreenW, height: kTitleViewH)
        let titles = ["推荐","游戏","娱乐","趣玩"]
        let titleView = PageTitleView(frame: titleFrame, titles: titles)
        titleView.delegate = self
        return titleView
    }()
    
    private lazy var pageContentView : PageContentView = {[weak self] in
        //1.确定内容的frame
        let contentH = kScreenH - kStatuseBarH - kNavigationBarH - kTitleViewH
        let contentFrame = CGRect(x: 0, y: kStatuseBarH + kNavigationBarH + kTitleViewH, width: kScreenW, height: contentH)
        //2.确定所有的子控制器
        var childVcs = [UIViewController]()
        for _ in 0..<4{
            let vc = UIViewController()
            vc.view.backgroundColor = UIColor(r: CGFloat( arc4random_uniform(255)), g: CGFloat(arc4random_uniform(255)), b: CGFloat(arc4random_uniform(255)))
            childVcs.append(vc)
        }
        
        let contentView = PageContentView(frame: contentFrame, childVcs: childVcs, parentViewController: self)
        contentView.delegate = self
        return contentView
    
    
    }()
    
    // MARK - 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置UI界面
        setupUI()
        
    }


    

}

extension HomeViewController {
    private func setupUI(){
        //0. 不需要跳转UISrollView的内边距
        automaticallyAdjustsScrollViewInsets = false
        //1.设置导航栏
        setupNavgationBar()
        
        //2.添加TitleView
        view.addSubview(papTitleView)
        
        //3.添加contentView
        view.addSubview(pageContentView)
        pageContentView.backgroundColor = UIColor.purple
    }
    private func setupNavgationBar(){
        let size = CGSize(width: 40, height: 40)

        //1.设置左侧的item
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName:"",size: size)
        
        //2.设置右侧的item
        let historyItem = UIBarButtonItem(imageName: "", highImageName: "", size: size)
        let searchItem = UIBarButtonItem(imageName: "", highImageName: "", size: size)
        let qrcodeItem = UIBarButtonItem(imageName: "", highImageName: "", size: size)
        navigationItem.rightBarButtonItems = [historyItem,searchItem,qrcodeItem]
        
    }
    
}
// MARK: - 遵守PageTitleViewDelegate协议
extension HomeViewController :PageTitleViewDelegate {
    func pageTitleView(titleView: PageTitleView, selectedIndex index: Int) {

        pageContentView.setCurrentIndex(currentIndex: index)
    }
}

// MARK: - 遵守PageContentViewDelegate协议
extension HomeViewController:PageContentViewDelegate {
    func pageContentView(contentView: PageContentView, progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        papTitleView.setTitleWithProgress(progress: progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
    }
}
